<?php 
class CommonModel extends We7Table {

    public $errCode = 0;
    public $errMsg  = '';
    public $errField = '';
    protected function setError($code = 0, $msg = '系统繁忙，请稍后再试', $field = '') {
        $this->errCode = +(''. 101 . $code);
        $this->errMsg  = $msg;
        $this->errField = $field;
        return false;
    }

    /**
     * 保存排序，表必须为事务类型
     */
    public function sortList($sortArr = []) {

        if(empty($sortArr)) {
            return true;
        }

        pdo_begin();
        foreach ($sortArr as $item) {
            $res = pdo_update($this->tableName, ['sort'=>$item['sort']], ['id'=>$item['id']]);
            if($res === false) {
                pdo_rollback();
                return false;
            }
        }
        pdo_commit();

        return true;

    }

    /**
     * 删除项，支持多项删除
     * TODO: 是否存在整体一次性删除的方法
     */
    public function deleteRow($id) {
        if(is_string($id)) {
            $ids = getKeysArray($id);
        } else {
            $ids = $id;
        }

        // 开始逐条删除
        pdo_begin();
        foreach ($ids as $id) {
            $res = pdo_delete($this->tableName, ['id'=>$id]);
            if($res === false) {
                pdo_rollback();
                return false;
            }
        }
        pdo_commit();

        return true;

    }
    
}